// JavaScript Document

// it works only once ... figuering out ...
function ajax(url)
{
	var results = '';
	if(window.XMLHttpRequest)
	{
		ajax = new XMLHttpRequest();
	}
	else
	{
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	}
	ajax.onreadystatechange = function()
	{
		if(ajax.readyState == 4 && ajax.status == 200)
		{
			results = ajax.responseText;
		}
	}
	ajax.open("GET",url,false);
	ajax.send();
	return (results);
}

// check Password
function matchPassword()
{
	var frm	= document.signup;
	var pw1 = frm.password.value;
	var pw2	= frm.password2.value;
	if(pw1.length < 7 )
	{
		document.getElementById("match").innerHTML = '<small><span style="color:red;">Must be at least 7 characters!</span></small>';
		return false;
	}
	else if(pw1 != pw2)
	{
		document.getElementById("match").innerHTML = '<small><span style="color:red;">Not Matched!</span></small>';
		return false;
	}
	else
	{
		document.getElementById("match").innerHTML = '<small><span style="color:green;">Matched!</span></small>';
		return true;
	}
}

// New User
function addUser()
{
	var frm 	= document.signup;
	var email	= frm.email;
	var fields	= frm.elements.length;
	var good	= 0;
	var texts 	= 0;
	var	chkmail = /^[a-zA-Z0-9._\+-]+@[a-zA-Z0-9]+([.-]?[a-zA-Z0-9]+)?([\.]{1}[a-zA-Z]{2,4}){1,4}$/; 
	
	//count all fields to be filled
	for(var i=0; i<fields; i++)
	{
		if(frm.elements[i].type == 'text' || frm.elements[i].type == 'password')
		{
			texts += 1;
		}
	}
	
	//check if any field is blank		
	for(var i=0; i<fields; i++)
	{
		if(frm.elements[i].type == 'text' || frm.elements[i].type == 'password')
		{
			if(frm.elements[i].value == '')
			{
				frm.elements[i].focus();
				break;
			}
			else
			{
				good += 1;
			}
		}
	}	
	
	// if all fields are filled, let's check if it's valid
	if(good == texts && good >0)
	{
		// NOt working well, so i chage to != false. will look back again soon
		if(chkmail.test(email) != false)
		{
			alert("Invalid Email Address!");
			email.focus();
		}
		else
		{
			if(matchPassword())
			{				
				//frm.password.value = calcMD5(pw1);
				//frm.password2.value = calcMD5(pw2);
				frm.submit();
			}
		}
	}
}

// ********** Not Yet working bec of ajax
// check available username
function checkName(val)
{
	var url = "http://localhost/bizbuddy.com/site/class/controller.php?type=getUsers&user="+val;
	var result = ajax(url);
	var divN = document.getElementById("checkN");
	document.getElementById("checkN").innerHTML = result;
	if(result == 'existed')
	{
		divN.style.color = "red";
	}
	else
	{
		divN.style.color = "green";
	}
}

// SignIn
function login()
{
	var frm = document.signin;
	if(frm.name.value == '' || frm.password.value == '')
	{
		alert("Incorrect! Missing Data.");
	}
	else
	{		
		//frm.password.value = calcMD5(frm.password.value);
                frm.password.value = frm.password.value;
		frm.submit();
	}
}
function signOut()
{
	window.location = "/bizbuddy.com/site/class/controller.php?type=logout";
}
	
function newCategory()
{
	var newCat = document.getElementById('newwCat').value;
	if(newCat != '')
	{
		ajax("http://localhost/bizbuddy.com/site/class/controller.php?type=newCat&value="+newCat);
		document.getElementById("add").click();
		document.getElementById("loadCats").click();
	}
}

function addAnnouncement()
{
	var frm 	= document.newAnnouncement;
	var subject	= frm.subject.value;
	var cats	= frm.chosenCats.value;
	var title	= /^[-/+=:\.\\]/
	
	if(title.test(subject) == true)
	{
		alert("Invalid Subject!");
	}
	else if(subject == '')
	{
		alert("Missing Subject!");
		frm.subject.focus();
	}
	else if(cats == '')
	{
		alert("Missing Category!\nPlease choose at least one.");
	}
	else
	{
		frm.submit();
	}
}

function addBusiness()
{
	var frm = document.newBusiness;
	var subject	= frm.name.value;
	var cats	= frm.chosenCats.value;
	var title	= /^[-/+=:\.\\]/
	
	if(title.test(subject) == true)
	{
		alert("Invalid Subject!");
	}
	else if(subject == '')
	{
		alert("Missing Subject!");
		frm.subject.focus();
	}
	else if(cats == '')
	{
		alert("Missing Category!\nPlease choose at least one.");
	}
	else
	{
		frm.submit();
	}
}

//get value of checked cats
function chooseCats()
{
	var frm		= document.forms.item(0);
	var allElm	= frm.elements.length;
	var cats 	= frm.chosenCats.value;
	checks	= '';
	for(var i = 0; i<allElm; i++)
	{
		if(frm.elements[i].type == 'checkbox')
		{
			if(frm.elements[i].checked)
			{
				checks += frm.elements[i].value+',';
			}
		}
	}
	if(checks != '')
		frm.chosenCats.value = checks;
}

//make chosen categories checked
function checkCats()
{
	var frm		= document.forms.item(0);
	var allElm	= frm.elements.length;
	var cats 	= frm.chosenCats.value;
	var cat	;
	var pos		= -1;
	do
	{
		cat = cats;
		cat = cat.substring(pos+1,cats.indexOf(',',pos+1));
		pos	= cats.indexOf(',',pos+1);
		
		for(var i = 0; i<allElm; i++)
		{
			if(frm.elements[i].type == 'checkbox')
			{
				if(frm.elements[i].value == cat)
					{
						frm.elements[i].checked = true;
					}
			}
		}
	}while(pos != -1);
}

function action(what,from,who)
{
	var url = 'http://localhost/bizbuddy.com/site/class/controller.php?type=action&what='+what+'&from='+from+'&who='+who;
	//alert(url);
	if(confirm("Are You Sure?"))
	{
		ajax(url);
		window.location.reload();
	}
}